package com.logic.landseaserver.models.prepayment.ws;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.models.prepayment.bean.AccountBalanceResp;
import com.logic.landseaserver.models.prepayment.bean.AccountChargebackResp;
import com.logic.landseaserver.models.prepayment.bean.AccountRechargeResp;
import com.logic.landseaserver.models.prepayment.bean.AccountResp;
import com.logic.landseaserver.models.prepayment.bean.AcotBalaForWCResp;
import com.logic.landseaserver.models.prepayment.bean.AddAccountObject;
import com.logic.landseaserver.models.prepayment.bean.AuthCodeIntoReq;
import com.logic.landseaserver.models.prepayment.bean.SelectEffectiveCouponReq;
import com.logic.landseaserver.models.prepayment.service.AutoPayBillService;
import com.logic.landseaserver.models.prepayment.service.PrepaymentService;
import com.logic.landseaserver.ws.dto.QueryCouponUesdDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Path("/prepayment")
@Api(value = "预付款 账户余额")
public  class PrepaymentRest
{
    private static final Logger LOGGER = LoggerFactory.getLogger(PrepaymentRest.class);
    
    @Autowired
    private PrepaymentService prepaymentService;
    
    @Autowired
    private AutoPayBillService autoPayBillService;
    
    
    @POST
    @Path("/getAccountList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询账户列表")
    public Response getAccountList(@ApiParam(value = "param", required = false) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<AccountResp> list = new ArrayList<AccountResp>();
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            list = prepaymentService.getAccountList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setResultTotalCount(totalCount);
            map.setData(list);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getAccountList", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/getAccountDetail/{accountId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询账户详情")
    public Response getAccountDetail(
        @ApiParam(value = "param", required = true) @PathParam("accountId") Integer accountId)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Map<String, Object> respMap  = prepaymentService.getAccountDetail(accountId);
            map.setData(respMap);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getAccountDetail", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/getAccountBalanceList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询账户信息列表")
    public Response getAccountBalanceList(@ApiParam(value = "param", required = false) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<AccountBalanceResp> list = new ArrayList<AccountBalanceResp>();
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            list = prepaymentService.getAccountBalanceList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setResultTotalCount(totalCount);
            map.setData(list);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getAccountBalanceList", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/getAccountBalanceForWechat")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "微信端查询账户信息")
    public Response getAccountBalanceForWechat()
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            List<AcotBalaForWCResp> list = prepaymentService.getAccountBalanceForWechat();
            map.setData(list);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getAccountBalanceForWechat", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST            
    @Path("/canUseAuthCode")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "可用的授权码")
    public Response canUseAuthCode(@ApiParam(value = "param", required = false) 
    SelectEffectiveCouponReq req)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            List<QueryCouponUesdDTO> list = prepaymentService.canUseAuthCode(req);
            map.setData(list);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "canUseAuthCode", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/authCodeInto")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "授权码的转入")
    public Response authCodeInto(@ApiParam(value = "param", required = false) AuthCodeIntoReq req)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            prepaymentService.authCodeInto(req);
            
            // 余额自动抵扣,实时发送金额变动消息
            autoPayBillService.autoPay(req.getAccountId(), null, true);
            
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "authCodeInto", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/getAccountRechargeList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询充值记录列表")
    public Response getAccountRechargeList(@ApiParam(value = "param", required = false) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<AccountRechargeResp> list = new ArrayList<AccountRechargeResp>();
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            list = prepaymentService.getAccountRechargeList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setResultTotalCount(totalCount);
            map.setData(list);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getAccountRechargeList", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/getAccountChargebackList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询抵扣记录列表")
    public Response getAccountChargebackList(@ApiParam(value = "param", required = false) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<AccountChargebackResp> list = new ArrayList<AccountChargebackResp>();
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            list = prepaymentService.getAccountChargebackList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setResultTotalCount(totalCount);
            map.setData(list);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "getAccountChargebackList", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/accountRechargeExcel")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "导出账户充值记录")
    public Response accountRechargeExcel(@ApiParam(value = "param", required = true) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            String url = prepaymentService.accountRechargeExcel(queryUtil);
            HashMap<String, String> resultmap = new HashMap<String, String>();
            resultmap.put("url", url);
            map.setData(resultmap);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "accountRechargeExcel", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/accountChargebackExcel")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "导出账户扣款记录")
    public Response accountChargebackExcel(@ApiParam(value = "param", required = true) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            String url = prepaymentService.accountChargebackExcel(queryUtil);
            HashMap<String, String> resultmap = new HashMap<String, String>();
            resultmap.put("url", url);
            map.setData(resultmap);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "accountChargebackExcel", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/addAccount")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "测试生成账户")
    public Response addAccount(@ApiParam(value = "param", required = false) AddAccountObject req)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            prepaymentService.addAccount(req);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(LOGGER, "addAccount", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/createAccount")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "生成已经签过约合同的账户")
    public Response createAccount(@ApiParam(value = "param", required = true) @Context HttpServletRequest request)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            String contractStatus= request.getParameter("contractStatus");
            String authKey= request.getParameter("AUTH_KEY");
            
            if ("logic123".equals(authKey))
            {
                LOGGER.info("createAccount|生成合同的账户.");
                if ("END".equals(contractStatus))
                {
                    LOGGER.info("createAccount|生成已结束合同的账户.");
                    prepaymentService.createAccountOfEnd();
                }
                else 
                {
                    LOGGER.info("createAccount|生成签约中合同的账户.");
                    prepaymentService.createAccountOfSign();
                }
                
            }
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            LOGGER.info("createAccount|生成账户失败.", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @GET
    @Path("/sendDbWechatMsg")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "测试发送自动抵扣发送微信消息")
    public Response sendDbWechatMsg()
    {
        CommonResultMap map = new CommonResultMap();
        
        autoPayBillService.sendDbWechatMsg();
        
        map.setResult(CommonResult.SUCCESS);
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
